package com.dean.weixinmediapicker;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dmcbig.mediapicker.PickerActivity;
import com.dmcbig.mediapicker.PickerConfig;
import com.dmcbig.mediapicker.entity.Media;

import java.util.ArrayList;

/**
 *  Dean
 * 仿微信图片选择器，基于https://github.com/dmcBig/MediaPickerPoject修改
 * 使用glide4.0,支持7.0及以上
 *
 * 修改： （仿微信，基本实现微信98%相似度）
 * 1.修改返回resultcode为java默认RESULT_CODE(-1)
 * 2.增加点击图片进入预览界面
 * 3.增加预览界面缩略图显示(分为“预览”按钮进入和点击图片进入两种模式)
 * 4.增加缩略图随大图滚动而滑动，选中未选中效果等
 * 5.增加缩略图拖动改变位置，并且发送顺序也相应改变
 * 6.其他细节(文字，提示信息等)改变
 */

public class MainActivity extends AppCompatActivity {

    private final static int PERMISSION_CODE = 100;

    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.btn_open);
        tvResult = (TextView) findViewById(R.id.tv_re);
        button.setOnClickListener(v -> {
            RequestPermission();
        });

    }

    private void RequestPermission() {
        //判断是否有权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            //去申请权限
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_CODE);

        } else {
            openPic();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_CODE) {
            for (int i=0; i<permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    //申请成功
                    openPic();
                } else {
                    //申请失败
                    Toast.makeText(MainActivity.this,"请打开权限后操作",Toast.LENGTH_LONG).show();
                }
            }
        }


    }

    private void openPic() {
        Intent intent = new Intent(this, PickerActivity.class);
        //PickerConfig.PICKER_IMAGE_VIDEO
        intent.putExtra(PickerConfig.SELECT_MODE,PickerConfig.PICKER_IMAGE_VIDEO);//default image and video (Optional)
        long maxSize=50*1024*1024L;//设置最大可选大小，50M
        intent.putExtra(PickerConfig.MAX_SELECT_SIZE,maxSize); //default 180MB (Optional)
        intent.putExtra(PickerConfig.MAX_SELECT_COUNT,9);  //default 40 (Optional)
        startActivityForResult(intent,10001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode==RESULT_OK && requestCode==10001) {
            tvResult.setText("返回:\n");
            int i = 0;
            //mediaType=1 图片  mediaType=3 视频
            ArrayList<Media> piclist = data.getParcelableArrayListExtra(PickerConfig.EXTRA_RESULT);

            for (Media media : piclist) {
                tvResult.append(++i + "->"+media.path+"\n");
            }
        }


    }
}
